package com.leyou.item.controller;

import com.github.pagehelper.PageInfo;
import com.leyou.common.entity.MyPage;
import com.leyou.item.entity.TbBrand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandCnotroller {


    @Autowired
    private BrandService brandService;

    ///page?key=&page=1&rows=5&sortBy=id&desc=false
//    @GetMapping(value = "page",produces = "text/html;charset=utf-8")
    @GetMapping("page")
    public ResponseEntity<MyPage> queryBrandList(String key,
                                                 @RequestParam(defaultValue = "1")Integer page,
                                                 @RequestParam(defaultValue = "5")Integer rows,
                                                 String sortBy,
                                                 @RequestParam(defaultValue = "false") Boolean desc
                                            ){
        PageInfo<TbBrand> tbBrandPageInfo = brandService.queryBrandList(key, page, rows, sortBy, desc);
        if(tbBrandPageInfo==null||tbBrandPageInfo.getSize()<1){
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        MyPage myPage = new MyPage(tbBrandPageInfo.getTotal(), tbBrandPageInfo.getList());
        return ResponseEntity.ok(myPage);
    }

    //添加
    @PostMapping("addBrand")
    public ResponseEntity<Void> addBrand(TbBrand brand,Long[] cids){
        try{
            brandService.addBrand(brand,cids);
            //添加成功
            //new ResponseEntity<>(HttpStatus.CREATED);
            return ResponseEntity.status(HttpStatus.OK).build();

        }catch (Exception e){
            //内部服务器出错
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    //修改
//    @PutMapping
    //修改
    @PostMapping("updateBrand")
    public ResponseEntity<Void> updateBrand(TbBrand brand,Long[] cids){
        try{
            brandService.updateBrand(brand,cids);
            //添加成功
            //new ResponseEntity<>(HttpStatus.CREATED);
            return ResponseEntity.status(HttpStatus.OK).build();

        }catch (Exception e){
            //内部服务器出错
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /*删除*/
    @GetMapping("deleteBrand")
    public ResponseEntity<Void> deleteBrand(Long[] bid){
        try {
            brandService.deleteBrand(bid);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }

    }

    //根据cid获取信息
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<TbBrand>> queryBrandByCid(@PathVariable("cid") Long cid){
        List<TbBrand> tbBrands = brandService.queryBrandByCid(cid);
        if(tbBrands==null ){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(tbBrands);
    }

    //根据品牌id查询品牌对象
    @GetMapping("/{bid}")
    public ResponseEntity<TbBrand> queryBrandById(@PathVariable("bid")Long bid){
        TbBrand brand=brandService.queryBrandById(bid);
        if (brand==null){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败417
        }
        return ResponseEntity.ok(brand);

    }
}
